package com.thermofisher.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

@Slf4j
@Component
public class B100Http
{

    private final static int CONNECTION_TIMEOUT = 6000;

    private final static int SO_TIMEOUT = 6000;

    private final static int CONNECTION_REQ_TIMEOUT = 15000;

    public String getToker(String requestUrl, Map<String, String> paramMap)
    {

        CloseableHttpClient httpclient = HttpClients.createDefault();
        try
        {
            URIBuilder uriBuilder = null;
            uriBuilder = new URIBuilder(requestUrl);
            for (String key : paramMap.keySet())
            {
                uriBuilder.addParameter(key, paramMap.get(key));
            }
            HttpPost httpPost = new HttpPost(uriBuilder.build());
            //请求超时
            //setConfig,添加配置,如设置请求超时时间,连接超时时间
            RequestConfig reqConfig = RequestConfig.custom()
                    .setConnectionRequestTimeout(CONNECTION_REQ_TIMEOUT)
                    .setSocketTimeout(SO_TIMEOUT).setConnectTimeout(CONNECTION_TIMEOUT).build();
            httpPost.setConfig(reqConfig);

            log.info("Fas POST 请求地址:" + requestUrl + ",param = " + paramMap);
            CloseableHttpResponse response = httpclient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {//如果状态码为200,就是正常返回
                String message = EntityUtils.toString(response.getEntity(), "UTF-8");
                response.close();
                return message;
            }
            else
            {
                log.info("返回码：" + response.getStatusLine().getStatusCode());
                response.close();
                return null;
            }
        }
        catch (Exception e)
        {
            log.error(e.getLocalizedMessage());
        }
        return null;
    }

    public String getData(String url, String token)
    {

        CloseableHttpClient client = HttpClients.createDefault();
        //默认post请求
        url = url.replaceAll(" ", "");
        HttpGet get = new HttpGet(url);

        try
        {
            get.addHeader("Content-Type", "text/plain;charset=UTF-8");
            get.setHeader("Accept", "application/json");
            get.setHeader("Authorization", "Bearer " + token);
            HttpResponse httpResponse = client.execute(get);
            HttpEntity entity = httpResponse.getEntity();
//            System.err.println("状态:" + httpResponse.getStatusLine());
//            System.err.println("参数:" + EntityUtils.toString(entity));
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK)
            {
                String message = EntityUtils.toString(entity);
                return message;
            }
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        catch (ClientProtocolException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;

    }

}
